package com.jams.goodox.widget;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jams.goodox.R;
import com.jams.goodox.entity.MeetDateBean;
import com.jams.goodox.entity.MeetMonthDate;
import com.jams.library.commonAdapter.RecycleCommonAdapter;
import com.jams.library.commonAdapter.RecycleCommonViewHolder;
import com.jams.library.listener.OnRecycleItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义的会议日历控件
 * Created by jams_zhen on 2017/5/3.
 */

public class MeetCalendar extends LinearLayout {

    private RecyclerView weekRecycle;
    private RecyclerView monthRecycle;

    private RecycleCommonAdapter monthAdapter;

    private List<String> weekStrList;

    private List<MeetMonthDate> monthDates;//后台返回的月份列表数组

    private MeetDateBean checkDate;//选择的日期

    private InterOnDateCheck interOnDateCheck;

    private boolean isShowOverCrossDate = false;//此处标识是否显示越界的日期

    private Context context;

    public MeetCalendar(Context context) {
        super(context);
        initView(context);
    }

    public MeetCalendar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public MeetCalendar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    /**
     * 将布局放置到控件上
     *
     * @param context
     */
    private void initView(Context context) {
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.meet_calender_recycle, this, true);
        weekRecycle = (RecyclerView) findViewById(R.id.meet_recycle_week);
        monthRecycle = (RecyclerView) findViewById(R.id.meet_recycle_month);
        monthDates = new ArrayList<>();//初始化填充数据列表
        setWeekAdapter();
        setMonthAdapter();
    }

    public void setCheckDate(MeetDateBean meetDateBean){
        checkDate=meetDateBean;
    }

    private void setWeekAdapter() {
        weekStrList = getListByStr(context.getResources().getStringArray(R.array.meet_week_title));
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 7);
        weekRecycle.setLayoutManager(gridLayoutManager);
        RecycleCommonAdapter weekAdapter = new RecycleCommonAdapter<String>(context, weekStrList, R.layout.meet_calender_week_item) {
            @Override
            public void convert(RecycleCommonViewHolder helper, String item, int position) {
                TextView txWeek = helper.getView(R.id.tx_week);
                txWeek.setText(item);
                if ("日".equalsIgnoreCase(item) || "六".equalsIgnoreCase(item)) {
                    txWeek.setTextColor(Color.parseColor("#ff6e0b"));
                } else {
                    txWeek.setTextColor(Color.parseColor("#333333"));
                }
            }
        };
        weekRecycle.setAdapter(weekAdapter);
    }

    private void setMonthAdapter() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setAutoMeasureEnabled(true);
        monthRecycle.setLayoutManager(linearLayoutManager);
        monthRecycle.addItemDecoration(new SimpleDividerItemDecoration(context));
        monthAdapter = new RecycleCommonAdapter<MeetMonthDate>(context, monthDates, R.layout.meet_month_item) {
            @Override
            public void convert(RecycleCommonViewHolder helper, MeetMonthDate item, int position) {
                helper.setText(R.id.tx_month_of_year, item.getMonthOfYear());
                RecyclerView dataRecyclerView = helper.getView(R.id.recycle_data);
                if (item.getMeetDateBeenList() == null || item.getMeetDateBeenList().size() == 0)
                    return;
                setDateAdapter(dataRecyclerView, item);
            }
        };
        monthRecycle.setAdapter(monthAdapter);
    }

    private void setDateAdapter(RecyclerView dataRecyclerView, MeetMonthDate item) {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 7);
        gridLayoutManager.setAutoMeasureEnabled(true);
        dataRecyclerView.setLayoutManager(gridLayoutManager);
        RecycleCommonAdapter dateAdapter = new RecycleCommonAdapter<MeetDateBean>(context, item.getMeetDateBeenList(), R.layout.meet_calender_date_item) {
            @Override
            public void convert(final RecycleCommonViewHolder helper, final MeetDateBean item, int position) {
                helper.setText(R.id.tx_date, item.getDateStr());

                TextView txDate = helper.getView(R.id.tx_date);
                if (!item.isCanReserve()) {
                    txDate.setTextColor(Color.parseColor("#aaaaaa"));
                } else {
                    if (item.getDayOfWeek() == 1 || item.getDayOfWeek() == 7) {
                        txDate.setTextColor(Color.parseColor("#ff6e0b"));
                    } else {
                        txDate.setTextColor(Color.parseColor("#333333"));
                    }
                }

                final TextView txIsReserve = helper.getView(R.id.tx_is_reserve);
                txIsReserve.setVisibility((!item.isCanReserve()) ? INVISIBLE : VISIBLE);
                txIsReserve.setText(item.isHavePlace() ? "有" : "无");
                txIsReserve.setTextColor(item.isHavePlace() ? Color.parseColor("#0096ff") : Color.parseColor("#aaaaaa"));
                helper.setBackground(R.id.linear_date, R.drawable.bg_cancel_white);
                //显示选中的日期
                if (checkDate != null&&!"0".equalsIgnoreCase(checkDate.getFormatDate())) {
                    if (checkDate.getFormatDate().equalsIgnoreCase(item.getFormatDate())&&item.isHavePlace()) {
                        helper.setBackground(R.id.linear_date, R.drawable.bg_cancel_theme);
                        txIsReserve.setTextColor(Color.parseColor("#333333"));
                    }
                }

                helper.setOnItemClickListener(new OnRecycleItemClickListener() {
                    @Override
                    public void itemClick() {
                        if (!item.isCanReserve() || !item.isHavePlace()) return;//不可预定or无场地，直接返回
                        //此处更新显示是否选中
                        helper.setBackground(R.id.linear_date, R.drawable.bg_cancel_theme);
                        txIsReserve.setTextColor(Color.parseColor("#333333"));

                        if (interOnDateCheck != null) {
                            interOnDateCheck.onCalenderClick(item);
                        }
                    }
                });
            }
        };
        dataRecyclerView.setAdapter(dateAdapter);
    }

    public void setMeetMonthList(List<MeetMonthDate> meetMonthList) {
        if (meetMonthList == null || meetMonthList.size() == 0) return;
        for (int i = 0; i < meetMonthList.size(); i++) {
            List<MeetDateBean> addMeetDateList = meetMonthList.get(i).getMeetDateBeenList();
            if (addMeetDateList == null || addMeetDateList.size() < 28 || isShowOverCrossDate)
                break;
            int startNum = addMeetDateList.get(0).getDayOfWeek();
            int endNum = addMeetDateList.get(addMeetDateList.size() - 1).getDayOfWeek();
            if (addMeetDateList.size() == 28 && startNum == 0) {//如果刚好是2月且正好是周日，就将该数据源直接拿来用
                break;
            }
            for (int j = 1; j < startNum; j++) {//补头
                addMeetDateList.add(0, new MeetDateBean());
            }
            for (int k = 7; k > endNum; k--) {
                addMeetDateList.add(new MeetDateBean());
            }
        }
        this.monthDates.clear();
        this.monthDates.addAll(meetMonthList);
        monthAdapter.notifyDataSetChanged();
    }

    private List<String> getListByStr(String[] weekStrs) {
        List<String> weekList = new ArrayList<>();
        if (weekStrs == null || weekStrs.length == 0) return weekList;
        for (String str : weekStrs) {
            weekList.add(str);
        }
        return weekList;
    }

    public void setInterOnDateCheck(InterOnDateCheck onDateCheck) {
        interOnDateCheck = onDateCheck;
    }

    public interface InterOnDateCheck {
        void onCalenderClick(MeetDateBean checkDate);
    }

}
